package org.gap.jseed.reflect;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class ClassAnnotationIterator extends AbstractClassIterator implements Iterator<Annotation>, Iterable<Annotation> {
	private Iterator<Annotation> iterator;
	
	public ClassAnnotationIterator(Class<?> javaClass) {
		Collection<Class<?>> classHierarchy = getClassHierarchy(javaClass);
		List<Annotation> methods = getAllClassAnnotations(classHierarchy);
		
		iterator = methods.iterator();
	}

	private List<Annotation> getAllClassAnnotations(Collection<Class<?>> hierarchy) {
		List<Annotation> result = new ArrayList<Annotation>();
		for (Class<?> each : hierarchy) {
			result.addAll(Arrays.asList(each.getAnnotations()));
		}
		return result;
	}

	@Override
	public boolean hasNext() {
		return iterator.hasNext();
	}

	@Override
	public Annotation next() {
		return iterator.next();
	}

	@Override
	public void remove() {
	}

	@Override
	public Iterator<Annotation> iterator() {
		return this;
	}

}
